1. Field of the Invention
The present invention relates to the design of programming languages for computer systems. More specifically, the present invention relates to a method and an apparatus for importing static members of a class into a compilation unit, so that the static members can be referred to without prefixes within the compilation unit.
2. Related Art
Many object-oriented programming languages allow classes to export static members, also known as class methods, class variables, and member classes. When a program invokes or accesses a static member from outside its own class, the programmer must specify the static member's class of origin. The requirement to provide these prefixes is a great annoyance to programmers and hinders the readability of the program. The need for such prefixes has historically been regarded as a necessary evil.
For example, in the JAVA™ programming language, when applying functions to arguments of type “float” or “double”, it is necessary to write Math.abs(x), Math.sqrt(x), and Math.max(a, b), where “Math” refers to the class whose full name is “java.lang.Math”. This is undesirably verbose for numerical code compared to what one can write in languages such as C, Fortran, and Pascal: abs(x), sqrt(x), and max(a, b).
Furthermore, it is desirable in many situations to have a set of named constants. For example, for a traffic light the following named constants are useful: public static final int RED=0; public static final int YELLOW=1; and public static final int GREEN=2. It is undesirably verbose simply to put them in some utility class TrafficLight and refer to them always as TrafficLight.RED, TrafficLight.YELLOW, and TrafficLight.GREEN. Therefore, some programmers have taken to defining such constants in a utility “interface”, and then importing such constants into a class by the stratagem of declaring the class to implement the (otherwise vacuous) interface. However, inheriting from an interface to get access to constants in an implementation is a publicly visible operation, and is therefore subject to all the rules for binary compatibility. Moreover, systems that support “Write Once, Run Anywhere” (WORA) applications require all other implementations to inherit the same interface and the same constants, even though other implementations may not require them.
Hence, what is needed is a method and an apparatus that facilitates efficiently importing static members of a class into a compilation unit so that the static members can be referred to without prefixes within the compilation unit, and without affecting the exported Application Programming Interfaces (APIs) of the classes contained in the compilation unit.